<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>1-验证Diffing算法</title>
    </head>
    <body>
        <!-- 准备好DOM容器 -->
        <div id="test"></div>

        <!-- 必须按照顺序引入JS -->
        <!-- 引入React核心库 -->
        <script src="../js/react.development.js"></script>
        <!-- 引入react-dom库，用于操作DOM -->
        <script src="../js/react-dom.development.js"></script>
        <!-- 引入babel，用于将JSX翻译为JS -->
        <script src="../js/babel.min.js"></script>
        <script type="text/babel">
            class Time extends React.Component {
                state = { date: new Date() };

                componentDidMount() {
                    setInterval(() => {
                        this.setState({ date: new Date() });
                    }, 1000);
                }

                render() {
                    // 如果Diffing算法不存在，那么在两个input输入框中输入内容，每过一秒钟就会清空（因为生成了新的input DOM替换掉原来的）
                    // 但是这并没有发生，证明Diffing算法存在，且其比较的粒度为DOM node
                    return (
                        <div>
                            <h1>Hello</h1>
                            <input type="text" />
                            <span>
                                现在是：{this.state.date.toTimeString()}
                                <input type="text" />
                            </span>
                        </div>
                    );
                }
            }

            ReactDOM.render(<Time />, document.getElementById("test"));
        </script>
    </body>
</html>
